package Homework.Date7_03;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import Homework.Date7_03.DBUtil;

//对应s_subject的所有操作
//返回结果的方式
public class SubjectDao {
	// CRUD
	/**
	 * 修改
	 * 
	 * @param s
	 * @return
	 */
	public int update(Subject s) {
		String sql = "update s_subject set subjectname=?,classhour=?,gradeid=? where id=?";
		int rs = 0;
		try (Connection con = DBUtil.getConnection(); PreparedStatement ps = con.prepareStatement(sql);) {
			ps.setString(1, s.getSubjectname());
			ps.setInt(2, s.getClasshour());
			ps.setLong(3, s.getGradeid());
			ps.setLong(4, s.getId());
			rs = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 删除
	 * 
	 * @param id
	 * @return
	 */
	public int del(long id) {
		String sql = "DELETE FROM s_subject WHERE id=?";
		int rs = 0;
		try (Connection con = DBUtil.getConnection(); PreparedStatement ps = con.prepareStatement(sql);) {
			ps.setLong(1, id);
			rs = ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 增加
	 * 
	 * @param s
	 * @return
	 */
	public int add(Subject s) {
		String sql = "insert into s_subject(subjectname,classhour,gradeid) values(?,?,?)";
		int rs = 0;
		try (Connection con = DBUtil.getConnection(); PreparedStatement ps = con.prepareStatement(sql);) {
			ps.setString(1, s.getSubjectname());
			ps.setInt(2, s.getClasshour());
			ps.setLong(3, s.getGradeid());
			rs = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
		return rs;
	}

	// >>>R
	// 1:查询全部
	public List<Subject> search() {
		List<Subject> datas = new java.util.ArrayList<>();
		try (Connection con = DBUtil.getConnection();
				PreparedStatement ps = con.prepareStatement("select * from s_subject");
				ResultSet rs = ps.executeQuery();) {
			while (rs.next()) {
				datas.add(new Subject(rs.getLong("id"), rs.getString("subjectname"), rs.getInt("classhour"),
						rs.getLong("gradeid")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return datas;
	}

	// 分页查询
	public List<Subject> search(int pageNo, int pageSize) {
		int offset = (pageNo - 1) * pageSize;
		List<Subject> datas = new java.util.ArrayList<>();

		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = DBUtil.getConnection();
			ps = con.prepareStatement("select * from s_subject order by id limit ?,?");
			ps.setInt(1, offset);
			ps.setInt(2, pageSize);
			rs = ps.executeQuery();
			while (rs.next()) {
				datas.add(new Subject(rs.getLong("id"), rs.getString("subjectname"), rs.getInt("classhour"),
						rs.getLong("gradeid")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, ps, con);
		}
		return datas;
	}

	// 查询单个:找不到是null,找到了,返回一个对象
	public Subject get(long id) {
		Subject s = null;
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;

		try {
			con = DBUtil.getConnection();
			ps = con.prepareStatement("select * from s_subject where id=?");
			ps.setLong(1, id);
			rs = ps.executeQuery();
			if (rs.next()) {
				s = new Subject(id, rs.getString("subjectname"), rs.getInt("classhour"), rs.getLong("gradeid"));
			}
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			DBUtil.close(rs, ps, con);
		}
		return s;
	}
}
